package com.parking.controller;

import com.parking.entity.ParkingLot;
import com.parking.entity.ParkingOrder;
import com.parking.entity.ParkingSpace;
import com.parking.service.ParkingService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api")
public class ParkingController {

    private final ParkingService parkingService;

    public ParkingController(ParkingService parkingService) {
        this.parkingService = parkingService;
    }

    // ParkingLot endpoints
    @PostMapping("/parking-lots")
    public ResponseEntity<ParkingLot> createParkingLot(@RequestBody ParkingLot parkingLot) {
        ParkingLot savedLot = parkingService.createParkingLot(parkingLot);
        return new ResponseEntity<>(savedLot, HttpStatus.CREATED);
    }

    @GetMapping("/parking-lots")
    public ResponseEntity<List<ParkingLot>> getAllParkingLots() {
        List<ParkingLot> parkingLots = parkingService.getAllParkingLots();
        return new ResponseEntity<>(parkingLots, HttpStatus.OK);
    }

    @GetMapping("/parking-lots/{id}")
    public ResponseEntity<ParkingLot> getParkingLotById(@PathVariable Long id) {
        Optional<ParkingLot> parkingLot = parkingService.getParkingLotById(id);
        return parkingLot.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @DeleteMapping("/parking-lots/{id}")
    public ResponseEntity<Void> deleteParkingLot(@PathVariable Long id) {
        parkingService.deleteParkingLot(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    // ParkingSpace endpoints
    @PostMapping("/parking-lots/{lotId}/spaces")
    public ResponseEntity<ParkingSpace> createParkingSpace(@PathVariable Long lotId,
                                                           @RequestBody ParkingSpace space) {
        ParkingSpace savedSpace = parkingService.createParkingSpace(lotId, space);
        return new ResponseEntity<>(savedSpace, HttpStatus.CREATED);
    }

    @GetMapping("/parking-lots/{lotId}/spaces")
    public ResponseEntity<List<ParkingSpace>> getParkingSpacesByLotId(@PathVariable Long lotId) {
        List<ParkingSpace> spaces = parkingService.getParkingSpacesByLotId(lotId);
        return new ResponseEntity<>(spaces, HttpStatus.OK);
    }

    @GetMapping("/parking-lots/{lotId}/spaces/available")
    public ResponseEntity<List<ParkingSpace>> getAvailableSpacesByLotId(@PathVariable Long lotId) {
        List<ParkingSpace> spaces = parkingService.getAvailableSpacesByLotId(lotId);
        return new ResponseEntity<>(spaces, HttpStatus.OK);
    }

    @PostMapping("/spaces/{spaceId}/occupy")
    public ResponseEntity<ParkingSpace> occupySpace(@PathVariable Long spaceId,
                                                    @RequestParam String licensePlate) {
        ParkingSpace occupiedSpace = parkingService.occupySpace(spaceId, licensePlate);
        return new ResponseEntity<>(occupiedSpace, HttpStatus.OK);
    }

    @PostMapping("/spaces/{spaceId}/release")
    public ResponseEntity<ParkingSpace> releaseSpace(@PathVariable Long spaceId) {
        ParkingSpace releasedSpace = parkingService.releaseSpace(spaceId);
        return new ResponseEntity<>(releasedSpace, HttpStatus.OK);
    }

    // ParkingOrder endpoints
    @PostMapping("/orders")
    public ResponseEntity<ParkingOrder> createOrder(@RequestParam Long spaceId,
                                                    @RequestParam String licensePlate) {
        ParkingOrder order = parkingService.createOrder(spaceId, licensePlate);
        return new ResponseEntity<>(order, HttpStatus.CREATED);
    }

    @PostMapping("/orders/{spaceId}/complete")
    public ResponseEntity<ParkingOrder> completeOrder(@PathVariable Long spaceId) {
        ParkingOrder order = parkingService.completeOrder(spaceId);
        return new ResponseEntity<>(order, HttpStatus.OK);
    }

    @GetMapping("/orders/license/{licensePlate}")
    public ResponseEntity<List<ParkingOrder>> getOrdersByLicensePlate(@PathVariable String licensePlate) {
        List<ParkingOrder> orders = parkingService.getOrdersByLicensePlate(licensePlate);
        return new ResponseEntity<>(orders, HttpStatus.OK);
    }
}